// pages/login/index.js
var app = getApp()
import _config from '../../utils/config';
import _fetch from '../../utils/httpService';
import util from '../../utils/util';
import {
  hexMD5
} from '../../utils/md5';
let timer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: 0,
    buttonText: '下一步',
    phonePlaceHolder: '请输入手机号',
    focusPhoneNumber: false,
    valuePhoneNumber: '',
    valuePhoneNumberTemp: '',
    isPhoneNumberClearShow: false,
    focusVerify: false,
    valueVerify: '',
    valueVerifyTemp: '',
    verifyText: '获取验证码',
    isVerifyEnable: false,
    isLoginEnabled: false,
    showModalPhoneNumberInput: false,
    showModalVerifyInput: false,
    phoneDisabled: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let valuePhoneNumber = this.data.tab === 0 ? app.globalData.userData.staff.mobile : this.data.valuePhoneNumber
    let isPhoneNumberClearShow = false;
    let isVerifyEnable = false;
    let isLoginEnabled = false;
    if (valuePhoneNumber) {
      // isPhoneNumberClearShow = true;
      isVerifyEnable = true;
    }
    this.setData({
      valuePhoneNumber: valuePhoneNumber,
      valuePhoneNumberTemp: valuePhoneNumber,
      isPhoneNumberClearShow: isPhoneNumberClearShow,
      isVerifyEnable: isVerifyEnable,
      isLoginEnabled: isLoginEnabled,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // if (timer) {
    //   this.setData({
    //     verifyText: '获取验证码',
    //     isVerifyEnable: true,
    //   });
    //   clearInterval(timer);
    // }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (timer) {
      this.setData({
        verifyText: '获取验证码',
        isVerifyEnable: true,
      });
      clearInterval(timer);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '助力企业工单管理智能升级', // 分享标题
      desc: '', // 分享描述
      path: '/pages/login/index', // 分享路径
      imageUrl: '../../img/Mask.png' // 分享图片
    }
  },
  // 启动定时器
  startTimer: function () {
    let vm = this;
    let second = 60;
    vm.setData({
      verifyText: --second + 's',
      isVerifyEnable: false,
    });
    timer = setInterval(
      function () {
        if (second < 1) {
          vm.setData({
            verifyText: '获取验证码',
            isVerifyEnable: true,
          });
          clearInterval(timer);
        } else {
          vm.setData({
            verifyText: --second + 's',
          });
        }
      },
      1000
    );
  },
  // 获取验证码
  fetchVerifyCode: function () {
    let vm = this;
    let sign = String(this.data.valuePhoneNumber) + String(Date.now()) + '2go8gnciqusrpzthmjqr2jvwykp6qxhh'
    let requestData = {
      mobile: this.data.valuePhoneNumber,
      timestamp: Date.now(),
      sign: hexMD5(sign)
    }
    vm.startTimer();
    _fetch.post(_config.url.get_valid_code, requestData)
      .then(function (data) {}).catch(function (error) {
        wx.showToast({
          title: '获取验证码失败',
          icon: 'none',
        });
      });
  },
  // 点击手机号输入框事件
  tapPhoneNumberBox: function (event) {
    this.setData({
      focusPhoneNumber: true,
      focusVerify: false,
    });
  },
  // 点击验证码输入框事件
  tapVerifyBox: function (event) {
    this.setData({
      focusPhoneNumber: false,
      focusVerify: true,
    });
  },
  // 输入手机号事件
  inputPhoneNumber: function (event) {
    if (event.detail.value) {
      let isLoginEnabled = false;
      if (this.data.valueVerify) {
        isLoginEnabled = true;
      }
      this.setData({
        valuePhoneNumber: event.detail.value,
        isPhoneNumberClearShow: this.data.tab === 0 ? false : true,
        isVerifyEnable: true,
        isLoginEnabled: isLoginEnabled,
      });
    } else {
      this.setData({
        valuePhoneNumber: event.detail.value,
        isPhoneNumberClearShow: false,
        isVerifyEnable: false,
        isLoginEnabled: false,
      });
    }
  },
  // 输入验证码事件
  inputVerify: function (event) {
    if (event.detail.value) {
      let isLoginEnabled = false;
      if (this.data.valuePhoneNumber) {
        isLoginEnabled = true;
      }
      this.setData({
        valueVerify: event.detail.value,
        valueVerifyTemp: event.detail.value,
        isLoginEnabled: isLoginEnabled,
      });
    } else {
      this.setData({
        valueVerify: event.detail.value,
        valueVerifyTemp: event.detail.value,
        isLoginEnabled: false,
      });
    }
  },
  // 点击手机号清空事件
  tapPhoneNumberClear: function (event) {
    this.setData({
      valuePhoneNumber: '',
      valuePhoneNumberTemp: '',
      isPhoneNumberClearShow: false,
      isVerifyEnable: false,
      isLoginEnabled: false,
    });
  },
  // 点击获取验证码事件
  tapVerify: function (event) {
    if (this.data.verifyText != '获取验证码') {
      return;
    }
    let isPhoneNumberOk = util.checkMobilePhoneNumber(this.data.valuePhoneNumber);
    if (!isPhoneNumberOk) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none'
      })
      return;
    }
    this.fetchVerifyCode();
  },
  // 点击输入手机号确定事件
  phoneNumberInputConfirm: function (event) {
    this.setData({
      focusPhoneNumber: true,
      showModalPhoneNumberInput: false,
    });
  },
  // 点击输入验证码确定事件
  verifyInputConfirm: function (event) {
    this.setData({
      focusVerify: true,
      showModalVerifyInput: false,
    });
  },
  tapAction() {
    let vm = this
    let requestData = {
      mobile: this.data.valuePhoneNumber,
      code: this.data.valueVerify
    }
    if (this.data.tab === 0) {
      _fetch.post(_config.url.valid_code_info, requestData).then(() => {
        vm.setData({
          tab: 1,
          valuePhoneNumber: '',
          valuePhoneNumberTemp: '',
          valueVerify: '',
          valueVerifyTemp: '',
          phoneDisabled: false,
          buttonText: '完成',
          verifyText: '获取验证码'
        })
        clearInterval(timer);
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none',
        });
      })
    } else {
      _fetch.put(_config.url.rebind, requestData).then(() => {
        wx.redirectTo({
          url: '../mine/index'
        })
        wx.showToast({
          title: '成功更换绑定手机号',
          icon: 'none',
        });
        vm.setData({
          tab: 0,
          valuePhoneNumber: '',
          valuePhoneNumberTemp: '',
          valueVerify: '',
          valueVerifyTemp: '',
          phoneDisabled: true,
          buttonText: '下一步',
          verifyText: '获取验证码'
        })
        clearInterval(timer);
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'none',
        });
      })
    }
  }
})